import {
	baseUrl,
} from '@/common/base';

// 判断对象是否为空对象
const isEmpty = obj => Reflect.ownKeys(obj).length === 0 && obj.constructor === Object;

// 全局请求封装
// loading参数: title(标题)
export const request = ({
	url = baseUrl.http,
	method = 'GET',
	params,
	loading = {},
}) => {
	// const token = uni.getStorageSync('token');
	if (!isEmpty(loading)) {
		uni.showLoading({
			mask: true,
			...loading
		});
	}
	// if (!token) {
	// 	// 执行没有登录的逻辑
	// 	return;
	// }
	return new Promise((resolve, reject) => {
		uni.request({
			url,
			method: method,
			timeout: 20000,
			header: {
				// token: token
			},
			data: {
				...params
			},
			success(res) {
				if (baseUrl.http == url) {
					if (res.data.errno === 200) {
						resolve(res);
					} else {
						reject(res);
					}
				} else {
					resolve(res);
				}
			},
			fail(err) {
				console.log(err);
				reject(err);
			},
			complete() {
				if (!isEmpty(loading)) {
					uni.hideLoading();
				}
			}
		});
	});
};
